<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="任务描述" prop="description">
				<el-input v-model="form.description" placeholder="计划任务描述" clearable></el-input>
			</el-form-item>
			<el-form-item label="执行类名" prop="jobClassName">
				<el-input v-model="form.jobClassName" placeholder="例如：com.xunheng.quartz.app.job.HelloWorldJob" clearable></el-input>
			</el-form-item>
			<el-form-item label="定时规则" prop="cronExpression">
				<sc-cron v-model="form.cronExpression" placeholder="请输入Cron定时规则" clearable :shortcuts="shortcuts"></sc-cron>
			</el-form-item>
			<el-form-item label="参数" prop="parameter">
				<el-input v-model="form.parameter" placeholder="请输入参数" clearable></el-input>
			</el-form-item>
			<el-form-item label="是否启用" prop="status">
				<el-switch v-model="form.status" :active-value="0" :inactive-value="-1"></el-switch>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaving" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import scCron from '@/components/scCron';
	export default {
		components: {
			scCron
		},
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增计划任务',
					edit: '编辑计划任务'
				},
				form: {
					id:"",
					description: "",
					jobClassName: "",
					cronExpression: "",
					parameter:null,
					status: 0
				},
				rules: {
					description:[
						{required: true, message: '请填写任务描述'}
					],
					jobClassName:[
						{required: true, message: '请填写执行类名'}
					],
					cronExpression:[
						{required: true, message: '请填写定时规则'}
					]
				},
				visible: false,
				isSaving: false,
				shortcuts: [
					{
						text: "每天8点和12点 (自定义追加)",
						value: "0 0 8,12 * * ?"
					}
				]
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			//表单提交方法 com.xunheng.quartz.app.job.HelloWorldJob
			submit(){
				this.$refs.dialogForm.validate(async(valid) => {
					if (valid) {
						this.isSaving = true;
						var res;
						if(this.mode == 'add')res = await this.$API.system.task.save.post(this.form);
						else if(this.mode == 'edit')res = await this.$API.system.task.edit.post(this.form);
						if(res.success){
							this.visible = false;
							this.$message.success("操作成功")
							this.$emit('success', this.form, this.mode)
						}
						this.isSaving = false;
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.description = data.description
				this.form.jobClassName = data.jobClassName
				this.form.cronExpression = data.cronExpression
				this.form.parameter = data.parameter
				this.form.status = data.status
			}
		}
	}
</script>

<style>
</style>
